package com.lagou.web.servlet;

import com.lagou.utils.UUIDUtils;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.IOUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.UUID;


@WebServlet(name = "FileUploadServlet",urlPatterns = "/upload")
public class FileUploadServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            // 1.创建磁盘文件工厂对象
            DiskFileItemFactory factory = new DiskFileItemFactory();
            // 2.创建文件上传核心类
            ServletFileUpload upload = new ServletFileUpload(factory);
            // 2.1 设置文件上传编码
            upload.setHeaderEncoding("utf-8");
            // 2.2 判断表单是否是 文件上传表单
            boolean multipartContent = upload.isMultipartContent(request);
            // 2.3 是文件上传表单
            if(multipartContent){
            // 3.解析request --获取表单项的集合
                List<FileItem> list = upload.parseRequest(request);
                if (list!=null){
                    // 4.遍历集合 获取表单项目
                    for (FileItem fileItem : list) {
                        // 5.判断当前表单项 是不是普通表单项
                        boolean formField = fileItem.isFormField();
                        if(formField){
                            // 普通表单项
                            String fieldName = fileItem.getFieldName();
                            // 设置编码
                            String value = fileItem.getString("utf-8");
                            System.out.println(fieldName+"="+value);
                        }else{
                            // 文件上传项
                            // 获取文件名
                            String name = fileItem.getName();
                            // 拼接新的文件名 使用UUID保证不重复
                            String newFileName = UUIDUtils.getUUID()+"_"+name;
                            // 获取输入流
                            InputStream inputStream = fileItem.getInputStream();
                            // 获取项目的运行目录
                            String realPath = this.getServletContext().getRealPath("/");
                            // 截取到webapps的路径
                            String hello_work_war = realPath.substring(0, realPath.indexOf("hello_work_war"));
                            // 获取输出流
                            FileOutputStream fileOutputStream = new FileOutputStream(hello_work_war+"/upload/" + newFileName);
                            IOUtils.copy(inputStream,fileOutputStream);
                            fileOutputStream.close();
                            inputStream.close();

                        }
                    }
                }
            }
        } catch (FileUploadException e) {
            e.printStackTrace();
        }

        // 4.遍历表单项集合
        // 5.判断是普通表单项 还是文件上传项
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}
